//
//  FindViewController.m
//  kr_36
//
//  Created by lmt on 16/1/18.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FindViewController.h"
#import "LMTAFNetwork.h"
#import "LunboModel.h"
#import "LunBoTu.h"
#import "FindCollectionViewCell.h"

#import "RecentActivityController.h"
#import "BoutiqueViewController.h"
#import "RecommendViewController.h"
#import "FashionViewController.h"

#import "CompanyViewController.h"

@interface FindViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *lunArr;/**< 轮播图数据数组 */
@property (nonatomic, retain) UIView *lunboView;/**< 轮播图所在 */

@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSArray *collectionArr;/**< collectionView 名字 */
@property (nonatomic, retain) NSArray *imgArr;/**< 图片 */

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSArray *tableArr;/**< tableView名字 */
@end

@implementation FindViewController

- (void)dealloc
{
    [_lunArr release];
    [_lunboView release];
    [_collectionView release];
    [_collectionArr release];
    [_imgArr release];
    [_tableView release];
    [_tableArr release];
    
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"发现";
    
    // 创建轮播图
    [self creatLunbo];
    
    // 创建collectionView
    [self creatCollectionView];
    
    // 创建tableView
    [self creatTableView];
}

#pragma mark - 创建collectionView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + 150 + 170, self.view.frame.size.width, self.view.frame.size.height - 64 - 150 - 170 - 49) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellT"];
    
    self.tableArr = [NSArray arrayWithObjects:@"创业公司", @"我要融资", @"寻找投资人", nil];
    
    _tableView.separatorColor = [UIColor blueColor];

    
    [_tableView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellT" forIndexPath:indexPath];
    cell.textLabel.text = _tableArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        CompanyViewController *cpVC = [[CompanyViewController alloc] init];
        [self.navigationController pushViewController:cpVC animated:YES];
        [cpVC release];
    }
    
}

#pragma mark - 创建collectionView
- (void)creatCollectionView
{
    // layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 30) / 2, 70);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 + 150, self.view.frame.size.width, 170) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[FindCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    
    
    self.collectionArr = [NSArray arrayWithObjects:@"推荐", @"精选", @"弄潮创", @"近期活动", nil];
    self.imgArr = [NSArray arrayWithObjects:@"tuijian@3x", @"jingxuan@3x", @"chuang@3x", @"huodong@3x", nil];
    
    [layout release];
    [_collectionView release];
}

#pragma mark - collectionView方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _collectionArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FindCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.title.text = _collectionArr[indexPath.item];
    cell.img.image = [UIImage imageNamed:_imgArr[indexPath.item]];
    cell.layer.borderColor = [UIColor blueColor].CGColor;
    cell.layer.borderWidth = 1;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {
        RecommendViewController *reVC = [[RecommendViewController alloc] init];
        [self.navigationController pushViewController:reVC animated:YES];
        [reVC release];
    }
    
    if (indexPath.item == 1) {
        BoutiqueViewController *boVC = [[BoutiqueViewController alloc] init];
        [self.navigationController pushViewController:boVC animated:YES];
        [boVC release];
    }
    
    if (indexPath.item == 2) {
        FashionViewController *faVc = [[FashionViewController alloc] init];
        [self.navigationController pushViewController:faVc animated:YES];
        [faVc release];
    }
    
    if (indexPath.item == 3) {
        RecentActivityController *raVC = [[RecentActivityController alloc] init];
        [self.navigationController pushViewController:raVC animated:YES];
        [raVC release];
    }
}

#pragma mark - 创建轮播图
- (void)creatLunbo
{
    
    self.lunboView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 150)];
    [self.view addSubview:_lunboView];
    // 解析轮播图数据
    NSString *str = @"https://rong.36kr.com/api/mobi/roundpics";
    [LMTAFNetwork getUrl:str body:nil response:LMTJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
       
        NSMutableArray *arr = resposeObject[@"data"][@"pics"];
        self.lunArr = [LunboModel modelHanderWithArray:arr];
        // 将图片字符串传入新的数组
        NSMutableArray *a = [NSMutableArray array];
        for (LunboModel *m in _lunArr) {
            NSString *str = m.imgUrl;
            [a addObject:str];
        }
        // 创建轮播图
        LunBoTu *b = [[LunBoTu alloc] initWithFrame:_lunboView.bounds stringArray:a];
        [_lunboView addSubview:b];
        [b release];
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    [_lunboView release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
